import Store from 'common/Store'

export default class Http {
  static _ins: Http
  static get instance(): Http {
    return this._ins || new Http;
  }
  constructor() {
    Http._ins = this
  }

  moduleName = 'Http'
  /**
   * 平台登录
   */
  public pfLoginUrl = SERVER_ADDERSS + '/user/sdk/login'

  /**
   * facebookId 登录路径
   */
  public fbLoginUrl = SERVER_ADDERSS + '/user/fb/login'

  /**
   * 获取区服 地址
   */
  public zonesUrl = SERVER_ADDERSS + '/user/sdk/zones'

  /**
   * 获取角色 地址
   */
  public roleUrl = SERVER_ADDERSS + '/user/player/list'


  public fbLogin(code): JQuery.jqXHR {
    return $.ajax({
      type: 'GET',
      url: this.fbLoginUrl,
      data: {
        clientId: Store.instance.map.activityID,
        redirectUrl: Store.instance.map.fbRedirectUrl,
        code: code
      }
    })
  }

  public pfLogin(data): JQuery.jqXHR {
    return $.ajax({
      type: 'GET',
      url: this.pfLoginUrl,
      data
    })
  }

  public getZones(): JQuery.jqXHR {
    return $.ajax({
      type: 'GET',
      url: this.zonesUrl,
      data: {
        appId: Store.instance.map.activityID,
        // token: Store.instance.map.userInfo && Store.instance.map.userInfo.token
      }
    })
  }

  public getRole(data): JQuery.jqXHR {
    return $.ajax({
      type: 'GET',
      url: this.roleUrl,
      data
    })
  }

  public getActivity(path, data): JQuery.Deferred<EventData> {
    var promise = $.Deferred()
    $.ajax({
      type: 'GET',
      url: SERVER_ADDERSS + '/activity/' + path,
      data
    }).then(data => {
      promise.resolve({
        code: data.code,
        state: data.state
      })
    })
    return promise
  }
}